package controller;

import Utils.ConstantUtils;
import Utils.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description: redis操作
 * @Author: liujh
 * @Date:
 * @UpdateUser: liujh
 * @Date: 2020/09/09$ 14:48$
 * @UpdateRemark:
 * @Version: 1.0
 */
@Controller
@Slf4j
@RequestMapping(value = "redis")
public class RedisController {

    @Autowired
    RedisTemplate redisTemplate;


    @GetMapping(value = "session")
    @ResponseBody
    public Object getsession(){
         String key = (String) SecurityUtils.getSubject().getSession().getId();
          log.info("key:{}",key);
          if (redisTemplate.hasKey(key)){
              return ResponseData.success(ConstantUtils.CODE_STATUS_SUCCESS,ConstantUtils.SUCCESS);
          }
         /* String sessionId = redisTemplate.opsForValue().get(key);*/
          return ResponseData.fail(ConstantUtils.CODE_STATUS_FAIL,ConstantUtils.FAIL);
    }

    @GetMapping(value = "test")
    @ResponseBody
    public Object test(){
        redisTemplate.opsForValue().set("test8","15");
        String key = (String) redisTemplate.opsForValue().get("test8");
        log.info("key:{}",key);
        return "";
    }
}
